package com.intmall.flink.window;

import com.intmall.flink.operator.ClickSource;
import com.intmall.flink.operator.Event;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

import java.time.Duration;

public class WindowTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 生成水位线的周期
        env.getConfig().setAutoWatermarkInterval(100);

//        DataStream<Event> stream = env.fromElements(
//                        new Event("Mary", "./home", 1000L),
//                        new Event("Bob", "./cart", 2000L),
//                        new Event("Alice", "./cart", 2100L),
//                        new Event("Bob", "./product?id=1", 3000L),
//                        new Event("Bob", "./product?id=2", 3500L),
//                        new Event("Bob", "./product?id=3", 4000L),
//                        new Event("Bob", "./product?id=3", 3600L),
//                        new Event("Alice", "./product?id=1", 5100L),
//                        new Event("Mary", "./center", 6000L)
//                )
        DataStream<Event> stream = env.addSource(new ClickSource())
                .assignTimestampsAndWatermarks(WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                        .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                            @Override
                            public long extractTimestamp(Event element, long recordTimestamp) {
                                return element.getTimestamp();
                            }
                        }));

        stream.map(new MapFunction<Event, Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> map(Event value) throws Exception {
                        return Tuple2.of(value.getUser(), 1L);
                    }
                })
                .keyBy(data -> data.f0)
                // 滚动事件时间窗口
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                // 滑动事件
//                .window(SlidingEventTimeWindows.of(Time.hours(1), Time.minutes(5)))
                // 会话窗口
//                .window(EventTimeSessionWindows.withGap(Time.seconds(2)))
                // 计数窗口，每2个数滑动一次
//                .countWindow(10, 2)
                .reduce(new ReduceFunction<Tuple2<String, Long>>() {
                    @Override
                    public Tuple2<String, Long> reduce(Tuple2<String, Long> value1, Tuple2<String, Long> value2) throws Exception {
                        return Tuple2.of(value1.f0, value1.f1 + value2.f1);
                    }
                })
                .print();

        env.execute();
    }
}
